package com.yanqu.road.dao.impl.channelcenter.washe;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDao;
import com.yanqu.road.entity.activity.washe.config.WasheHonorMomentConfig;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class WasheHonorMomentConfigDaoImpl extends TempDao {

    private static String getWasheHonorMomentConfigSql = "select * from `t_n_washepvp_lifemax` where `activityId`=?";

    public Map<Integer, List<WasheHonorMomentConfig>> getWasheHonorMomentConfigMap(int activityId) {
        Map<Integer, List<WasheHonorMomentConfig>> map = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getChannelCenterDb());
            call = getDb().getConnection().prepareCall(getWasheHonorMomentConfigSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                WasheHonorMomentConfig washeHonorMomentConfig = new WasheHonorMomentConfig();
                washeHonorMomentConfig.setActivityId(rs.getInt("activityId"));
                washeHonorMomentConfig.setHonorMomentId(rs.getInt("honorMomentId"));
                washeHonorMomentConfig.setHonorMomentType(rs.getInt("honorMomentType"));
                washeHonorMomentConfig.setTitle(rs.getString("title"));
                washeHonorMomentConfig.setDesc(rs.getString("desc"));
                washeHonorMomentConfig.setCondition(rs.getInt("condition"));

                map.putIfAbsent(washeHonorMomentConfig.getHonorMomentType(), new ArrayList<>());
                map.get(washeHonorMomentConfig.getHonorMomentType()).add(washeHonorMomentConfig);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }

        for (List<WasheHonorMomentConfig> list : map.values()) {
            list.sort(Comparator.comparing(WasheHonorMomentConfig::getHonorMomentId));
        }
        return map;
    }

}
